﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObjects;
using System.Net;
using System.IO;
using GT5RemoteMobile.Services;

namespace GT5RemoteMobile
{
    public partial class friends : System.Web.UI.Page
    {
        protected string jsessionId;
        protected string psnid;
        protected List<string> friendIds = new List<string>();
        protected Dictionary<string, List<RemoteRaceDriver>> drivers;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["jsessionid"] == null)
                Response.Redirect("default.aspx");
            else
                jsessionId = (string)Session["jsessionid"];
            string[] friends = (string[])Session["friends"];

            psnid = (Session["psnUser"] as Profile).ID;
            this.drivers = new Dictionary<string,List<RemoteRaceDriver>>();

            bool success = false;
            int retryCounter = 5;

            foreach (string friend in friends)
            {
                friendIds.Add(friend);

                GetDrivers driverUtil = new GetDrivers();
                driverUtil.jsessionId = jsessionId;
                List<RemoteRaceDriver> rrDriver = new List<RemoteRaceDriver>();

                while (!success)
                {
                    try
                    {
                        if (retryCounter == 0)
                            break;
                        rrDriver = driverUtil.LookupDrivers(friend);
                        success = true;
                    }
                    catch (Exception)
                    {
                        success = false;
                        retryCounter--;
                    }
                }
                if (!success)
                    Response.Redirect("Default.aspx");
                success = false;
                retryCounter = 5;
                if (rrDriver.Count == 0)
                    rrDriver.Add(new RemoteRaceDriver());
                drivers.Add(friend, rrDriver);
            }
            
        }
    }
}